<template>
  <el-empty v-if="item===null">
    <el-button type="warning" @click="$router.push('/')">返回首页</el-button>
  </el-empty>
  <div class="container" v-else>
    <div class="left">
      <div class="image">
        <img :src="item.picture||require('@/assets/picture.png')" width="350px" height="350px" alt="">
      </div>
    </div>
    <div class="right">
      <h2>{{item.name}}</h2>
      <div class="detail">
        <div class="txt">
          单价&nbsp;:&nbsp;
          <span class="symbol">￥</span>
          <span class="price">{{item.price}}</span>
        </div>
        <div class="txt">
          拼团价&nbsp;:&nbsp;
          <span class="symbol">￥</span>
          <span class="price">{{item.groupPrice}}</span>
        </div>
        <div class="txt">成团人数&nbsp;:&nbsp;{{item.peoples}}</div>
        <div class="txt">商品编号&nbsp;:&nbsp;{{item.id}}</div>
        <div class="txt">库存&nbsp;:&nbsp;{{item.number}}</div>
        <div class="txt">上架时间&nbsp;:&nbsp;{{item.createTime}}</div>
        <el-input-number style="margin-right: 20px;margin-top: 10px" :min="1" v-model="number" size="small"></el-input-number>
        <el-button type="info" size="small" @click="addCart()">我要拼团</el-button>
      </div>
    </div>
    <div class="clear"></div>
    <div class="footer">
      <div class="tips">商品介绍&nbsp;:</div>
    </div>
    <div class="intro">{{item.intro}}</div>
  </div>
</template>

<script>
import { getDetail } from '@/api/product'
import { addCart } from '@/api/cart'
import { recordLog } from '@/api/recordLog'

export default {
  name: "Details",
  data() {
    return {
      item: null,
      number:1
    }
  },
  mounted() {
    this.loadDetail()
  },
  methods: {
    loadDetail() {
      if (this.$route.params.id == 0){
        this.$notify({
          type: 'warning',
          title: '提示',
          message: '请前往首页选择具体商品',
          duration: 5000,
          offset: 100
        })
        this.item=null
        return
      }
      getDetail(this.$route.params.id).then(response => {
        if (response.data.picture !== null) {
          response.data.picture = process.env.VUE_APP_BASE_API + response.data.picture
        }
        this.item = response.data
      })
    },
    addCart() {
      const item = this.item
      const cart = {
        name: item.name,
        number: this.number,
        username: this.$store.getters.username,
        price: item.price,
        groupPrice: item.groupPrice,
        picture: item.picture,
        total: this.number * item.price,
        groupTotal: this.number * item.groupPrice
      }
      addCart(cart).then(res => {
        recordLog({ username: this.$store.getters.username, operate: `添加购物车拼团:${item.name}` })
        this.$message({
          message: '已添加至购物车，拼团成功',
          type: 'success',
          center: true
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
.container {
  width: 80%;
  margin: 0 auto;
  .left {
    width: 50%;
    float: left;
    .image {
      padding-top: 20px;
      width: 350px;
      height: 350px;
      margin: 0 auto;
    }
  }
  .right {
    width: 40%;
    float: left;
    margin-bottom: 30px;
    .detail {
      background: #efefef;
      padding-left: 15px;
      padding-top: 10px;
      padding-bottom: 10px;
      .txt {
        height: 40px;
        line-height: 40px;
        .symbol {
          color: #ff0000;
        }
        .price {
          color: #f00;
          font-size: 25px;
        }
      }
    }
  }
  .clear {
    clear: both;
  }
  .footer {
    height: 40px;
    background: #efefef;
    .tips {
      height: 40px;
      width: 90px;
      line-height: 40px;
      color: #fff;
      background: #4d4552;
      text-align: center;
    }
  }
  .intro {
    border: #efefef 2px solid;
    height: 130px;
    padding: 10px;
    text-indent: 2em;
    line-height: 25px;
    font-family: Helvetica Neue,Helvetica,PingFang SC,Hiragino Sans GB,Microsoft YaHei,SimSun,sans-serif;
  }
}

.el-button--warning.is-active,
.el-button--warning:active {
  background: #4d4c50;
  border-color: #4d4c50;
  color: #fff;
}

.el-button--warning:focus,
.el-button--warning:hover {
  background: #575559;
  border-color: #575559;
  color: #fff;
}

.el-button--warning {
  color: #FFF;
  background-color: #4d4c50;
  border-color: #4d4c50;
}

.el-button--info.is-active,
.el-button--info:active {
  background: #f79d36;
  border-color: #f79d36;
  color: #fff;
}

.el-button--info:focus,
.el-button--info:hover {
  background: #f8a03b;
  border-color: #f8a03b;
  color: #fff;
}

.el-button--info {
  color: #FFF;
  background-color: #f79d36;
  border-color: #f79d36;
}
</style>
